package webservices;

import interfaces.ICourseManage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import webservices.helper.Helper;

public class CourseManageImpl implements ICourseManage {
	
	public String courseUpdate(String action, String courseId, String courseName, String description, String teacher){
		String returnMessage = "";
		Connection con = null;
		try {
			con = Helper.getDBConnection();
			
			if ("add".equals(action)){
				PreparedStatement pst = con.prepareStatement("insert into course(name, description, teacher) values(?, ?, ?)");
				pst.setString(1, courseName);
				pst.setString(2, description);
				pst.setString(3, teacher);
				pst.executeUpdate();
			} else if ("update".equals(action)){
				PreparedStatement pst = con.prepareStatement("update course set name=?, description=?, teacher=? where courseid=?");
				pst.setString(1, courseName);
				pst.setString(2, description);
				pst.setString(3, teacher);
				pst.setString(4, courseId);
				pst.executeUpdate();
			} else if ("delete".equals(action)){
				PreparedStatement pst = con.prepareStatement("delete from course where courseid=?");
				pst.setString(1, courseId);
				pst.executeUpdate();
			}
			returnMessage = "<response name='success' />";
		} catch (SQLException e) {
			e.printStackTrace();
			returnMessage = "<response name='fail' value ='课程操作失败：" + e.getLocalizedMessage() + "' />";
		} finally {
			try{
				con.close();
			}catch(SQLException e) {}
		}
		return returnMessage;
	}
	
}